﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using DTO;
using System.Data.SqlClient;
namespace DAL
{
    public class PhieuKham_DAL
    {
         private DataProvider _provider = new DataProvider();
	
		    public PhieuKham_DAL()
		    {
			    _provider.connect();
		    }
            public void ThemPhieuKham(PhieuKham_DTO pk)
            {
                string insertCommand = @"INSERT INTO PHIEUKHAM (MaHS, MaNV, Lankham, Ngaykham, Noidungkham, Ketluan) VALUES(" +
                       pk.MaHS + ", " +
                       pk.MaNV + ", " + pk.Lankham + ", '" + pk.Ngaykham + "', '" +  pk.Noidungkham +"', '" + pk.Ketluan + "')";

                _provider.executeNonQuery(insertCommand);
            }
            public List<PhieuKham_DTO> DS_PhieuKhamTheoMaHS(string MaHS)
            {
                // Một list các parameter
                List<SqlParameter> paramters = new List<SqlParameter>();
                paramters.Add(new SqlParameter("@MaHS", MaHS));



                // Gọi hàm excute sử dujg procedure có parameter
                SqlDataReader reader = (SqlDataReader)_provider.executeQueryParameter("DS_PhieuKhamTheoMaHS", paramters);

                List<PhieuKham_DTO> List_PK = new List<PhieuKham_DTO>();

                while (reader.Read())
                {
                    PhieuKham_DTO pk = new PhieuKham_DTO();
                    pk.SoPK = int.Parse(reader["SoPK"].ToString());
                    pk.MaHS = int.Parse(reader["MaHS"].ToString());
                    pk.MaNV = int.Parse(reader["MaNV"].ToString());
                    pk.Lankham = int.Parse(reader["Lankham"].ToString());
                    pk.Ngaykham = Convert.ToDateTime(reader["Ngaykham"]);
                    pk.Noidungkham = reader["Noidungkham"].ToString();
                    pk.Ketluan = reader["Ketluan"].ToString();
                  

                    List_PK.Add(pk);
  
                }

                reader.Close();
                return List_PK;
            }
            public List<PhieuKham_DTO> DS_PhieuKham()
            {

                SqlDataReader reader = (SqlDataReader)_provider.executeQuery("DS_PhieuKham");

                List<PhieuKham_DTO> List_PK = new List<PhieuKham_DTO>();

                while (reader.Read())
                {
                    PhieuKham_DTO pk = new PhieuKham_DTO();
                    pk.SoPK = int.Parse(reader["SoPK"].ToString());
                    pk.MaNV = int.Parse(reader["MaNV"].ToString());
                    pk.MaHS = int.Parse(reader["MaHS"].ToString());
                    pk.Lankham = int.Parse(reader["Lankham"].ToString());
                    pk.Ngaykham = Convert.ToDateTime(reader["Ngaykham"]);
                    pk.Noidungkham = reader["Noidungkham"].ToString();
                    pk.Ketluan = reader["Ketluan"].ToString();


                    List_PK.Add(pk);
                }

                reader.Close();
                return List_PK;
            }
            public void Xoa(PhieuKham_DTO pk)
            {
                string deleteCommand = "DELETE FROM PHIEUKHAM WHERE SoPK = " + pk.SoPK;
                
                _provider.executeNonQuery(deleteCommand);
              

            }


    }
}
